package gui;

import data.User;

import javax.swing.*;
import javax.swing.text.AbstractDocument;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.*;

public class AdmEdit extends JPanel {
	
	private JLabel lbl_Firstname;
	private JLabel lbl_Lastname;
	private JLabel lbl_CPRID;
	private JLabel lbl_PIN;
	private JLabel lbl_Header;
	private JTextField textField_Firstname;
	private JTextField textField_Lastname;
	private JTextField textField_CPRID;
	private JPasswordField passwordField_PIN;
	private JButton btn_Back;
	private JButton btn_Submit;
	private JTextField textField_Streetname;
	private JTextField textField_Zipcode;
	private JLabel lbl_Streetname;
	private JLabel lbl_Streetnr;
	private JTextField textField_Streetnr;
	private JLabel lbl_ZipCode;
	private JTextField textField_City;
	private JLabel lbl_City;
	private JLabel lbl_Note;
	private JTextField textField_Tlf;
	private JLabel lbl_Tlf;
	private JComboBox comboBox_Users;
	
	private Screen screen;
	
	public AdmEdit(Screen screen) {
		
		this.screen = screen;
		
		setPreferredSize(new Dimension(550, 400));
		setLayout(null);
		
		lbl_Firstname = new JLabel("Firstname:");
		lbl_Firstname.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Firstname.setBounds(241, 97, 82, 21);
		add(lbl_Firstname);
		
		lbl_Lastname = new JLabel("Lastname:");
		lbl_Lastname.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Lastname.setBounds(241, 135, 81, 21);
		add(lbl_Lastname);
		
		lbl_CPRID = new JLabel("ID:");
		lbl_CPRID.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_CPRID.setBounds(48, 99, 22, 21);
		add(lbl_CPRID);
		
		lbl_PIN = new JLabel("PIN:");
		lbl_PIN.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_PIN.setBounds(38, 135, 33, 21);
		add(lbl_PIN);
		
		lbl_Header = new JLabel("Edit User");
		lbl_Header.setFont(new Font("SansSerif", Font.BOLD, 26));
		lbl_Header.setBounds(218, 11, 123, 34);
		add(lbl_Header);
		
		textField_Firstname = new JTextField();
		textField_Firstname.addMouseListener(mouseListener);
		textField_Firstname.setToolTipText("characters only");
		textField_Firstname.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Firstname.setBounds(326, 97, 150, 25);
		add(textField_Firstname);
		textField_Firstname.setColumns(10);
		
		textField_Lastname = new JTextField();
		textField_Lastname.addMouseListener(mouseListener);
		textField_Lastname.setToolTipText("characters only");
		textField_Lastname.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Lastname.setBounds(326, 133, 150, 25);
		add(textField_Lastname);
		textField_Lastname.setColumns(10);
		
		textField_CPRID = new JTextField();
		textField_CPRID.setEditable(false);
		textField_CPRID.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_CPRID.setBounds(80, 97, 150, 25);
		add(textField_CPRID);
		textField_CPRID.setColumns(10);
		
		passwordField_PIN = new JPasswordField();
		passwordField_PIN.addMouseListener(mouseListener);
		passwordField_PIN.setToolTipText("4 digits only");
		((AbstractDocument)passwordField_PIN.getDocument()).setDocumentFilter(new LimitOfChar(4));
		passwordField_PIN.setFont(new Font("SansSerif", Font.PLAIN, 16));
		passwordField_PIN.setBounds(80, 133, 150, 25);
		add(passwordField_PIN);
		
		textField_Streetname = new JTextField();
		textField_Streetname.addMouseListener(mouseListener);
		textField_Streetname.setToolTipText("characters only");
		textField_Streetname.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Streetname.setBounds(326, 214, 150, 25);
		add(textField_Streetname);
		textField_Streetname.setColumns(10);
		
		textField_Zipcode = new JTextField();
		textField_Zipcode.addMouseListener(mouseListener);
		textField_Zipcode.setToolTipText("4 digits only");
		((AbstractDocument)textField_Zipcode.getDocument()).setDocumentFilter(new LimitOfChar(4));
		textField_Zipcode.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Zipcode.setBounds(326, 277, 150, 25);
		add(textField_Zipcode);
		textField_Zipcode.setColumns(10);
		
		textField_Streetnr = new JTextField();
		textField_Streetnr.addMouseListener(mouseListener);
		textField_Streetnr.setToolTipText("digits only allowed");
		textField_Streetnr.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Streetnr.setBounds(326, 243, 150, 25);
		add(textField_Streetnr);
		textField_Streetnr.setColumns(10);
		
		textField_City = new JTextField();
		textField_City.addMouseListener(mouseListener);
		textField_City.setToolTipText("characters only");
		textField_City.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_City.setBounds(326, 310, 150, 25);
		add(textField_City);
		textField_City.setColumns(10);
				
		textField_Tlf = new JTextField();
		textField_Tlf.addMouseListener(mouseListener);
		textField_Tlf.setToolTipText("12 digits only allowed");
		((AbstractDocument)textField_Tlf.getDocument()).setDocumentFilter(new LimitOfChar(12));
		textField_Tlf.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Tlf.setBounds(326, 180, 150, 25);
		add(textField_Tlf);
		textField_Tlf.setColumns(10);
		
		btn_Back = new JButton("Back");
		btn_Back.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Back.setBounds(38, 270, 100, 40);
		add(btn_Back);
		
		btn_Submit = new JButton("Submit");
		btn_Submit.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Submit.setBounds(369, 349, 108, 40);
		add(btn_Submit);

		lbl_Streetname = new JLabel("Streetname:");
		lbl_Streetname.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Streetname.setBounds(230, 216, 93, 21);
		add(lbl_Streetname);
		
		lbl_Streetnr = new JLabel("Street nr.:");
		lbl_Streetnr.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Streetnr.setBounds(248, 245, 75, 21);
		add(lbl_Streetnr);
		
		lbl_ZipCode = new JLabel("Zip code:");
		lbl_ZipCode.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_ZipCode.setBounds(252, 279, 71, 21);
		add(lbl_ZipCode);
		
		lbl_City = new JLabel("City:");
		lbl_City.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_City.setBounds(287, 312, 36, 21);
		add(lbl_City);
		
		lbl_Note = new JLabel("<html><p>Note: ID is not <br> editable </p></html>");
		lbl_Note.setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC, 16));
		lbl_Note.setBounds(38, 167, 185, 48);
		add(lbl_Note);
		
		lbl_Tlf = new JLabel("Telephone:");
		lbl_Tlf.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Tlf.setBounds(236, 182, 87, 21);
		add(lbl_Tlf);
		
		comboBox_Users = new JComboBox();
		comboBox_Users.setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC, 16));
		comboBox_Users.setBounds(80, 56, 150, 30);
		addAllUsers();
		add(comboBox_Users);
	}
		
	public void addActionListener(ActionListener event) {
		btn_Submit.addActionListener(event);
		btn_Back.addActionListener(event);
	}
	
	public void addItemListener(ItemListener item){
		comboBox_Users.addItemListener(item);
	}
	
	public void showUserDataFields(int CPR_ID) {
		for (User usr : screen.getClassATM().getClassBankDatabase().getUserList()) {
			if(usr.getCPR() == CPR_ID) {
				textField_CPRID.setText(""+usr.getCPR());
				passwordField_PIN.setText(""+usr.getPIN());
				textField_Firstname.setText(""+usr.getFirstName());
				textField_Lastname.setText(""+usr.getLastName());
				textField_Tlf.setText(""+usr.getTelephoneNr());
				textField_Streetname.setText(""+usr.getStreetName());
				textField_Streetnr.setText(""+usr.getStreetNr());
				textField_Zipcode.setText(""+usr.getZipCode());
				textField_City.setText(""+usr.getCity());
			}
		}
	}
	
	public void setAdminNotEditable() {
		textField_Firstname.setEditable(false);
		textField_Lastname.setEditable(false);
		textField_Tlf.setEditable(false);
		textField_Streetname.setEditable(false);
		textField_Streetnr.setEditable(false);
		textField_Zipcode.setEditable(false);
		textField_City.setEditable(false);
	}
	
	public void setEditable() {
		textField_Firstname.setEditable(true);
		textField_Lastname.setEditable(true);
		textField_Tlf.setEditable(true);
		textField_Streetname.setEditable(true);
		textField_Streetnr.setEditable(true);
		textField_Zipcode.setEditable(true);
		textField_City.setEditable(true);
	}
	
	public JButton getSubmit() {
		return btn_Submit;
	}
	
	public JButton getBack() {
		return btn_Back;
	}
	
	public JComboBox getComboBox() {
		return comboBox_Users;
	}

	public String getTextField_Firstname() {
		return textField_Firstname.getText();
	}

	public String getTextField_Lastname() {
		return textField_Lastname.getText();
	}

	public int getTextField_USERID() {
		return Integer.parseInt(textField_CPRID.getText().trim());
	}

	public int getPasswordField_USERPIN() {
		char[] pin = passwordField_PIN.getPassword();
		return Integer.parseInt(new String(pin));
	}

	public String getTextField_Streetname() {
		return textField_Streetname.getText();
	}

	public int getTextField_Zipcode() {
		return Integer.parseInt(textField_Zipcode.getText().trim());
	}

	public int getTextField_Streetnr() {
		return Integer.parseInt(textField_Streetnr.getText().trim());
	}
	
	public String getTextField_City() {
		return textField_City.getText();
	}
	
	public int getTextField_Tlf(){
		return Integer.parseInt(textField_Tlf.getText().trim());
	}
	
	public void addAllUsers(){
		comboBox_Users.removeAllItems();
		for (User usr : screen.getClassATM().getClassBankDatabase().getUserList()) {
			comboBox_Users.addItem("CPR ID: " + usr.getCPR());
		}
		screen.pack();
	}
	
	MouseListener mouseListener = new MouseAdapter(){    
	    final int defaultTimeout = ToolTipManager.sharedInstance().getInitialDelay();

	    @Override
	    public void mouseEntered(MouseEvent e) {
	        ToolTipManager.sharedInstance().setInitialDelay(0);
	    }

	    @Override
	    public void mouseExited(MouseEvent e) {
	        ToolTipManager.sharedInstance().setInitialDelay(defaultTimeout);
	    }
	};
}